package com.tgpms.common;

import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;


public class Base64Util {

	
	/**字符串base64加密
	 * @param str
	 * @return
	 */
	public static String ByteToBase64(byte[] b) {
		// BASE64加密
		Encoder encoder = Base64.getEncoder();
		byte[] data = encoder.encode(b);
		return new String(data);
	}
	
	
	/**字符串base64解密
	 * @param str
	 * @return
	 */
	public static byte[] Base64ToByte(String str) { 
		// BASE64解密
		Decoder decoder = Base64.getDecoder();
		byte[] bytes = decoder.decode(str.getBytes());
		for (int i = 0; i < bytes.length; ++i) {
			if (bytes[i] < 0) {// 调整异常数据
				bytes[i] += 256;
			}
		}
		return bytes;
	}
}
